<template>
  <PageLayout class="form-container">
    <a-card>
      <div class="form-title">{{ route.query.id ? "编辑" : "新增" }}角色</div>
      <div class="form-box w-600">
        <a-form :model="formState" :rules="rules" ref="formRef" layout="Vertical" :labelCol="{ style: { width: '80px' } }" :wrapperCol="{ style: { width: 'calc(100% - 80px)' } }">
          <a-form-item label="账号" name="username" required>
            <a-input v-model:value="formState.username" placeholder="请输入账号" :maxlength="50" />
          </a-form-item>
          <a-form-item label="姓名" name="nickname" required>
            <a-input v-model:value="formState.nickname" placeholder="请输入姓名" :maxlength="50" />
          </a-form-item>
          <a-form-item label="角色" name="roleIds" required>
            <a-select
              v-model:value="formState.roleIds"
              placeholder="请选择角色"
              mode="multiple"
              :options="roleList"
            />

            <!-- <a-tree-select v-model:value="formState.roleIds" style="width: 100%" :tree-data="treeData" tree-checkable showCheckedStrategy="SHOW_ALL" allow-clear placeholder="请选择角色" tree-node-filter-prop="label" /> -->
          </a-form-item>

          <a-button type="primary" class="float-right" style="width: 100px" @click="save">确定</a-button>
        </a-form>
      </div>
    </a-card>
  </PageLayout>
</template>

<script setup>
import { postUserAdd, postUserUpdate, postUserDetail } from "@/api/common";
import { postMenuTreeList } from "@/api/system";
import { useStoreCommonEnum,useStoreUser } from "@/store";
import { message } from "ant-design-vue";
import { onMounted } from "vue";
import { commonEncrypt } from "@/utils/common";
const storeUser = useStoreUser();
const storeCommonEnum = useStoreCommonEnum();

const route = useRoute();
const router = useRouter();
const formRef = ref(null);

const formState = reactive({
  id: "",
  username: "",
  nickname: "",
  roleIds: undefined,
  password: commonEncrypt('123456', storeUser.rsaKey),
  remark: "",
});
const roleList = ref([]);

function resetTreeData(tree) {
  let resultTree = [];
  tree.forEach((item) => {
    let btnList = item.childrenList?.filter((ele) => {
      return ele.menuType == "F";
    });
    let curComponent = {
      ...item,
      // children: item.childrenList,
      label: item.menuName,
      value: item.id,
      btnList,
    };
    console.log("curComponent", curComponent);
    curComponent.children = resetTreeData(item.childrenList);
    resultTree.push(curComponent);
  });

  return resultTree;
}

onMounted(async () => {
  roleList.value = await storeCommonEnum.getCommonEnum("role");
  // postMenuTreeList().then((res) => {
  //   treeData.value = JSON.parse(JSON.stringify(resetTreeData(res.data)));
  // });
  if (route.query.id) {
    postUserDetail({ id: route.query.id }).then((res) => {
      for (let key in formState) {
        formState[key] = res.data[key];
      }
      formState.roleIds = res.data.roleList?.map(item => item.id)
    });
  }
});

const rules = {
  username: [{ required: true, message: "请输入账号", trigger: "blur" }],
  nickname: [{ required: true, message: "请输入姓名", trigger: "blur" }],
  roleIds: [{ required: true, message: "请选择部门", trigger: "blur" }],
};

const save = () => {
  formRef.value.validate().then(() => {
    const requestUrl = formState.id ? postUserUpdate : postUserAdd;
    requestUrl({ ...formState }).then(() => {
      message.success(formState.id ? "编辑成功" : "新增成功");
      router.go(-1);
    });
  });
};
</script>

<style lang="less" scoped>
.btn-box {
  margin-top: 24px;
  margin-left: 130px;
  :deep(.ant-btn) {
    margin-right: 16px;
  }
}
</style>
